﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Umbrella.Whs.SharePoint;
using System.Diagnostics;

namespace Umbrella.Whs.WhsAddin.Wizards.SharePointInstallation
{
    internal class InstallStepSqlServerEmbeddedEdition : IInstallStep
    {
        private InstallationManager _instMgr = new InstallationManager();
        private DirectoryInfo _spDir;

        public string Title
        {
            get { return Resources.txtInstallingSSEE; }
        }

        public int Weight
        {
            get { return 100; }
        }

        public InstallStepSqlServerEmbeddedEdition(DirectoryInfo spCoreInstallerDir)
        {
            if (spCoreInstallerDir == null)
                throw new ArgumentNullException("spCoreInstallerDir");

            if (!spCoreInstallerDir.Exists)
                throw new ArgumentException("directory does not exist", "spCoreInstallerDir");

            _spDir = spCoreInstallerDir;
        }

        public void PerformInstall()
        {
            // run installation
            {
                var pi = new RunProcessLogged(
                    "msiexec",
                    "/i \"" + Path.Combine(_spDir.FullName, @"database\SSEE_10.msi") + 
                    "\" /qn /norestart /lv \"" + Path.Combine(InstallationManager.InstallationConfigAndLogDir, "SSEE.log")  + 
                    "\" CALLERID=OCSETUP.EXE",
                    null
                );
                
                pi.Run();
            }

            // set start mode to automatic
            {
                var pi = new RunProcessLogged(
                    "sc",
                    "config mssql$microsoft##ssee start= auto",
                    null
                );

                pi.Run();
            }

            // start service
            {
                var sc = new ServiceController("mssql$microsoft##ssee");

                if (sc.Status == ServiceControllerStatus.Stopped)
                    sc.Start();
            }
        }
    }
}
